package com.jerry.project;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.Entity;

public class Sort {
	private static String[] sortType={"createTime","startTime","hot"};
	public static void sortbyWhat(List<Entity> entities,String s,int i){
		if(s.equals(sortType[0])){
			sortbyCreateTime(entities,i);
		}
		else if(s.equals(sortType[1])){
			sortbyStartTime(entities,i);
		}
		else if(s.equals(sortType[2])){
			sortbyHot(entities,i);
		}
	}
	private static List<Entity> sortbyCreateTime(List<Entity> entities,final int i){
		Collections.sort(entities,new Comparator<Entity>() {
			public int compare(Entity e1, Entity e2) {
				Date d1=(Date) e1.getProperty("createTime");
				Date d2=(Date) e2.getProperty("createTime");
				if(d1.before(d2)){
					return i;
				}
				else if(d1.after(d2)){
					return -1*i;
				}
				else return 0;
			}
		});
		return entities;
	}
	
	private static List<Entity> sortbyStartTime(List<Entity> entities,final int i){
		Collections.sort(entities,new Comparator<Entity>() {
			public int compare(Entity e1, Entity e2) {
				Date d1=(Date) e1.getProperty("startTime");
				Date d2=(Date) e2.getProperty("startTime");
				if(d1.before(d2)){
					return -1*i;
				}
				else if(d1.after(d2)){
					return i;
				}
				else return 0;
			}
		});
		return entities;
	}
	
	private static List<Entity> sortbyHot(List<Entity> entities,final int i){
		Collections.sort(entities,new Comparator<Entity>() {
			public int compare(Entity e1, Entity e2) {
				int d1=Integer.parseInt(e1.getProperty("interestedNumber").toString());
				int d2=Integer.parseInt(e2.getProperty("interestedNumber").toString());
				if(d1<d2){
					return i;
				}
				else if(d1>d2){
					return -1*i;
				}
				else return 0;
			}
		});
		return entities;
	}
}
